﻿<script lang="ts" name="tickets" setup>
import { ref, reactive, onMounted } from 'vue';
import { ElMessage } from 'element-plus';
import type { FormRules } from 'element-plus';
import { formatDate } from '/@/utils/formatTime';
import { useticketsApi } from '/@/api/ticket/tickets';

//父级传递来的函数，用于回调
const emit = defineEmits(['reloadTable']);
const ticketsApi = useticketsApi();
const ruleFormRef = ref();

const state = reactive({
	title: '',
	loading: false,
	showDialog: false,
	ruleForm: {} as any,
	stores: {},
	dropdownData: {} as any,
});

function generateUniqueIdWithTime() {
	const timestamp = Date.now().toString();
	const randomPart = Math.random().toString(36).substr(2, 9);
	return `${randomPart}-${timestamp}`;
}

// 自行添加其他规则
const rules = ref<FormRules>({});

// 页面加载时
onMounted(async () => {
	// 示例调用

	const data = (await ticketsApi.getDropdownData(false).then((res) => res.data.result)) ?? {};
	state.dropdownData.masterId = data.masterId ?? [];
	state.dropdownData.productionId = data.productionId ?? [];
	state.dropdownData.processprogress = data.processprogress ?? [];
});

// 打开弹窗
const openDialog = async (row: any, title: string) => {
	state.title = title;
	row = row ?? {};
	state.ruleForm = row.id ? await ticketsApi.detail(row.id).then((res) => res.data.result) : JSON.parse(JSON.stringify(row));
	state.showDialog = true;
	if (row.id == null) {
		const uniqueId = generateUniqueIdWithTime();
		state.ruleForm.encode = uniqueId;
	}
};

// 关闭弹窗
const closeDialog = () => {
	emit('reloadTable');
	state.showDialog = false;
};

// 提交
const submit = async () => {
	ruleFormRef.value.validate(async (isValid: boolean, fields?: any) => {
		if (isValid) {
			let values = state.ruleForm;
			await ticketsApi[state.ruleForm.id ? 'update' : 'add'](values);
			closeDialog();
		} else {
			ElMessage({
				message: `表单有${Object.keys(fields).length}处验证失败，请修改后再提交`,
				type: 'error',
			});
		}
	});
};

//将属性或者函数暴露给父组件
defineExpose({ openDialog });
</script>
<template>
	<div class="tickets-container">
		<el-dialog v-model="state.showDialog" :width="800" draggable :close-on-click-modal="false">
			<template #header>
				<div style="color: #fff">
					<span>{{ state.title }}</span>
				</div>
			</template>
			<el-form :model="state.ruleForm" ref="ruleFormRef" label-width="auto" :rules="rules">
				<el-row :gutter="35">
					<el-form-item v-show="false">
						<el-input v-model="state.ruleForm.id" />
					</el-form-item>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="类型" prop="ticketstype">
							<g-sys-dict v-model="state.ruleForm.ticketstype" code="work _order _type" render-as="select" placeholder="请选择类型" clearable filterable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="产品主数据" prop="masterId">
							<el-select clearable filterable v-model="state.ruleForm.masterId" placeholder="请选择产品主数据">
								<el-option v-for="(item, index) in state.dropdownData.masterId" :key="index" :value="item.value" :label="item.label" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="产线" prop="productionId">
							<el-select clearable filterable v-model="state.ruleForm.productionId" placeholder="请选择产线">
								<el-option v-for="(item, index) in state.dropdownData.productionId" :key="index" :value="item.value" :label="item.label" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="数量" prop="quantity">
							<el-input-number v-model="state.ruleForm.quantity" placeholder="请输入数量" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="计划开始时间" prop="planstarttime">
							<el-date-picker v-model="state.ruleForm.planstarttime" type="date" placeholder="计划开始时间" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="计划结束时间" prop="planEndtime">
							<el-date-picker v-model="state.ruleForm.planEndtime" type="date" placeholder="计划结束时间" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="备注" prop="remark">
							<el-input v-model="state.ruleForm.remark" placeholder="请输入备注" maxlength="32" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="是否允许跳工序" prop="skipornot">
							<el-switch v-model="state.ruleForm.skipornot" active-text="允许" inactive-text="不允许" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="工序进度" prop="processprogress">
							<el-select clearable filterable v-model="state.ruleForm.processprogress" placeholder="请选择工序进度">
								<el-option v-for="(item, index) in state.dropdownData.processprogress" :key="index" :value="item.value" :label="item.label" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="编码" prop="encode">
							<el-input disabled v-model="state.ruleForm.encode" placeholder="系统自动生成" maxlength="100" show-word-limit clearable />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="() => (state.showDialog = false)">取 消</el-button>
					<el-button @click="submit" type="primary" v-reclick="1000">确 定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>
<style lang="scss" scoped>
:deep(.el-select),
:deep(.el-input-number) {
	width: 100%;
}
</style>